<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Http\Util\Site;
use App\Http\Util\Status;
use App\Model\BannerAd;
use App\Model\Info;
use App\Model\Picture;
use Illuminate\Support\Facades\DB;

trait ServiceArticle{

    public static function getList($title,$startTime,$endTime,$type,$limit,$site=null,$status = null){
        $model = DB::table('infos as a');
        if(!$site){
            $site = Site::WEB;
        }
        $model->where('a.site',$site);
        if($title){
            $model->where("a.title","like","%{$title}%");
        }
        if($type){
            $model->where("a.type_id",$type);
        }
        if($status){
            $model->where('a.status',$status);
        }
        if($startTime && $endTime){
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
            $model->whereBetween("a.created_at",[$startTime,$endTime]);
        }
        return $model->leftJoin('pictures as b','b.id','=','a.pic_id')->orderBy('a.id','desc')->select('a.*','b.small_img')->paginate($limit);
    }

    /**
     * 获取单个id记录
     * @author gaoyoulong
     * @param $id
     * @return mixed
     */
    public static function getOne($id) {
        $model = DB::table('infos as a');
        $model->leftJoin('pictures as b','a.pic_id','=','b.id');
        $model->where('a.id',$id);
        $model->select('a.*','b.img','b.small_img');
        return $model->first();
    }

    /**
     * 更新记录
     * @author gaoyoulong
     * @param $input
     * @param $id
     * @return mixed
     */
    public static function updateInfo($input, $id) {
        $res = Info::where('id',$id)->update($input);
        return $res !== false ? Result::success() : Result::failure();
    }

    /**
     * 更新记录
     * @author gaoyoulong
     * @param $input
     * @param $id
     * @return mixed
     */
    public static function updateArticle($input, $id) {
        $res = Info::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * 删除记录
     * @author gaoyoulong
     * @param $id
     * @return mixed
     */
    public static function deleteInfo($id) {
        $res = Info::where("id", $id)->delete();
        return $res !== false ? Result::success() : Result::failure();
    }

    /**
     * 禁用、启用
     * @author gaoyoulong
     * @param $id
     * @param $able
     * @return mixed
     */
    public static function able($id,$able) {
        Info::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * wap资讯列表
     * czs
     */
    public static function infoList($infoType,$limit) {
        $model = DB::table('infos as a')->where('a.site',2)->where('a.status',1);
        if($infoType){
            $model->where('a.type_id',$infoType);
        }
        $list = $model
            ->orderBy('a.updated_at','desc')
            ->leftJoin('info_types as b','b.id','=','a.type_id')
            ->leftJoin('pictures as c','c.id','=','a.pic_id')
            ->select('a.*','c.middle_img')
            ->paginate($limit);
        return Result::success()->setData($list);
    }
}


